package com.backend.controller;

import com.backend.conmon.response.CommonResponse2;
import com.backend.conmon.response.ICommonResponse2;
import com.backend.req.DemoReq;
import com.backend.resp.DemoResp;
import com.backend.utils.JacksonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

/**
 * @Description:
 * @Author: tanglingyu
 * @CreateDate: 2024/7/1 10:22
 * @Version: 1.0
 */
@RestController
@Slf4j
public class DemoController {

  @Autowired
  private RestTemplate restTemplate;

  @Value("${server.port:}")
  private String port;


  @GetMapping("/hello")
  @SuppressWarnings({"rawtypes"})
  public CommonResponse2 hello() {
    return CommonResponse2.success();
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  @GetMapping("/demo")
  public ICommonResponse2 demo() {
    DemoReq demoDto = new DemoReq();
    demoDto.setName("tanglyu");
    demoDto.setPassword("123456");
    final CommonResponse2 commonResponse2 = restTemplate.postForObject("http://127.0.0.1:" + Integer.parseInt(port) + "/aiot/demo1", demoDto, CommonResponse2.class);
    if (commonResponse2 != null && "0000".equals(commonResponse2.getCode())) {
      final Object res = commonResponse2.getDatas();
      final DemoResp demoResp = JacksonUtils.parseObject(JacksonUtils.toJsonString(res), DemoResp.class);
      return new CommonResponse2("0000", "success", demoResp);
    }
    return null;
  }

  @SuppressWarnings({"unchecked", "rawtypes"})
  @PostMapping("/demo1")
  public ICommonResponse2 demo1(@RequestBody DemoReq demoDto) {
    log.info("请求入参{}", JacksonUtils.toJsonString(demoDto));
    final DemoResp demoResp = new DemoResp();
    BeanUtils.copyProperties(demoDto, demoResp);
    return new CommonResponse2("0000", "模拟远程调用success", demoResp);
  }

}
